package com.sesame.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import freemarker.template.TemplateException;
import kim.sesame.framework.email.service.EmailTemplate;
import kim.sesame.common.web.controller.AbstractWebController;
import kim.sesame.common.result.ApiResult;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@CommonsLog
@RestController
//@RequestMapping("/")
public class EmailController extends AbstractWebController {

    @Autowired
    EmailTemplate emailTemplate;

    /**
     * 简单邮件
     * http://127.0.0.1:8055/sendSimpleMail
     */
    @RequestMapping("/sendSimpleMail")
    public ApiResult sendSimpleMail() {
        String to = "654499437@qq.com";
        String subject = "主题：简单邮件";
        String text = "测试邮件内容 : " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN);

        emailTemplate.sendSimpleMail(to, subject, text);

        return success();
    }

    /**
     * 发送html邮件
     * http://127.0.0.1:8055/sendhtml1
     */
    @RequestMapping("/sendhtml1")
    public ApiResult sendhtml1() throws MessagingException {
        String to = "654499437@qq.com";
        String subject = "主题：嵌入静态资源";
        String text = "<h1>标题标题标题标题标题</h1><br/><b>时间</b>" + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN);

        emailTemplate.sendSimpleHtml(to, subject, text);
        return success();
    }


    @RequestMapping("/sendhtml2")
    public ApiResult sendhtml2() throws MessagingException, IOException, TemplateException {
        String to = "654499437@qq.com";
        String subject = "主题：模板邮件";

        Map<String, Object> model = new HashMap<>();
        model.put("user", "johnny");
        model.put("time", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));

        emailTemplate.sendTemplateHtml(to, subject, "email/aa.html", model);

        return success();
    }

}
